/*eslint-disable*/
// @ts-nocheck

import {
  Cartesian3,
  ClassificationType,
  Color,
  CustomDataSource,
  HeightReference,
  HorizontalOrigin,
  Resource,
  VerticalOrigin,
  Viewer,
} from '@space8/czm-plugin';
import { useCesiumEvent } from '@/views/pilotZone/oneMap/cesium-common/cesium-event/useCesiumEvent';
import PolylineTrailMaterialProperty
  from '@/components/GlobalCesium/src/PolylineTrailMaterialProperty';
import {
  addBussImageLayers,
  addFlowReservoirs,
  addFlowRiver,
  loadAnimateStation,
} from '@/components/GlobalCesium/src/cesiumGlobe';

// 添加需要的地图数据
export function addMapData(viewer: Viewer) {
  addBussImageLayers();
  loadAnimateStation('demonstrate');
  addFlowReservoirs();
  //add3DTiles();

  addFlowRiver('demonstration'); // 示范区河道

  // 示范区河流标注
  Resource.fetchJson('data/water_shifanqu_label.geojson').then((response) => {
    response.features.forEach((feature) => {
      const coordinates = feature.geometry.coordinates;
      const position = Cartesian3.fromDegrees(coordinates[0], coordinates[1], 5);
      viewer.entities.add({
        position: position,
        label: {
          text: feature.properties.GQMC,
          font: '20px Microsoft YaHei',
          verticalOrigin: VerticalOrigin.BOTTOM,
          horizontalOrigin: HorizontalOrigin.CENTER,
          heightReference: HeightReference.RELATIVE_TO_3D_TILE,
        },
      });
    });
  });

  // 示范区-田块-面 -- 矢量贴倾斜摄影
  const tiankuaiDataSource = new CustomDataSource('tiankuai');
  viewer.dataSources.add(tiankuaiDataSource);
  Resource.fetchJson('data/tainkuai.geojson').then((response) => {
    response.features.forEach((feature) => {
      const positions = feature.geometry.coordinates[0][0].map((t) => Cartesian3.fromDegrees(t[0], t[1]));
      tiankuaiDataSource.entities.add({
        type: 'tiandi', // 自定义类型
        name: feature.properties.tiankuai1,
        polygon: {
          hierarchy: positions,
          material: Color.fromCssColorString('#ABFF8D').withAlpha(0.5),
          classificationType: ClassificationType.BOTH,
        },
        polyline: {
          positions: positions,
          material: Color.WHITE,
          width: 2,
        },
      });
    });
  });

  // 示范区渠道-线
  const shifanquDataSource = new CustomDataSource('shifanqu');
  viewer.dataSources.add(shifanquDataSource);
  Resource.fetchJson('data/qcwqx.geojson').then((response) => {
    response.features.forEach((feature) => {
      const positions = feature.geometry.coordinates[0].map((t) => Cartesian3.fromDegrees(t[0], t[1]));
      shifanquDataSource.entities.add({
        type: 'shifanqu', // 自定义类型
        properties: feature.properties,
        polyline: {
          positions: positions,
          width: getWidth(feature.properties['级别']),
          clampToGround: true,
          material: new PolylineTrailMaterialProperty({
            color: Color.fromCssColorString('#3ae4e3'),
            speed: 10,
          }),
          classificationType: ClassificationType.BOTH,
          zIndex: 2,
        },
      });
    });
  });

  viewer.scene.screenSpaceCameraController.maximumZoomDistance = 10000;

  // 注册相应的事件
  useCesiumEvent(viewer);
}

function getWidth(data: string) {
  if (data === '2') {
    return 20;
  } else if (data === '3') {
    return 10;
  } else {
    return 8;
  }
}
